feat: implement core/embed block for youtube videos#377
Merged
Conversation
|
Check out the recent updates to your Headless Platform preview environment:
Learn more about preview environments in our documentation. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a custom core/embed block renderer to support YouTube embeds with a privacy-friendly youtube-nocookie.com iframe, and ensures HTML is available for fallback rendering (e.g., for blocks/providers the app doesn’t explicitly handle).
Changes:
- Register a new
CoreEmbedblock component in the WordPress blocks registry. - Implement YouTube-specific embed rendering (iframe) with a fallback to
renderedHtml. - Extend the single blog post GraphQL query to fetch
renderedHtmland include the CoreEmbed fragment.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/wp-blocks/index.js | Registers the new CoreEmbed block in the blocks map used by the provider. |
| src/wp-blocks/core-embed.jsx | Adds the CoreEmbed renderer with YouTube URL parsing + HTML fallback + GraphQL fragment. |
| src/pages/blog/[slug].jsx | Updates the post query to fetch renderedHtml and include the CoreEmbed fragment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+10
to
+38
| function getYouTubeId(url) { | ||
| if (!url) return; | ||
| try { | ||
| // eslint-disable-next-line n/prefer-global/url | ||
| const parsed = new URL(url); | ||
| const host = parsed.hostname.replace(/^www\./, ""); | ||
| if (host === "youtu.be") { | ||
| return parsed.pathname.slice(1).split("/")[0] || undefined; | ||
| } | ||
|
|
||
| if (host.endsWith("youtube.com") || host.endsWith("youtube-nocookie.com")) { | ||
| if (parsed.pathname === "/watch") { | ||
| return parsed.searchParams.get("v"); | ||
| } | ||
|
|
||
| const match = parsed.pathname.match( | ||
| /^\/(?:embed|shorts|v)\/(?<id>[^/?#]+)/, | ||
| ); | ||
| if (match) return match.groups?.id; | ||
| } | ||
| } catch { | ||
| // Fall through to regex below | ||
| } | ||
|
|
||
| const fallback = url.match( | ||
| /(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/|v\/)|youtu\.be\/)(?<id>[\w-]{11})/, | ||
| ); | ||
| return fallback ? fallback.groups?.id : undefined; | ||
| } |
Comment on lines
+44
to
+46
| if (providerNameSlug === "youtube" || /youtu\.?be/i.test(url ?? "")) { | ||
| const videoId = getYouTubeId(url); | ||
| if (videoId) { |
Comment on lines
+50
to
+59
| {/* eslint-disable-next-line react/iframe-missing-sandbox */} | ||
| <iframe | ||
| className="absolute inset-0 h-full w-full border-0" | ||
| src={`https://www.youtube-nocookie.com/embed/${videoId}`} | ||
| title={caption || "YouTube video"} | ||
| loading="lazy" | ||
| allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" | ||
| referrerPolicy="strict-origin-when-cross-origin" | ||
| allowFullScreen | ||
| /> |
| name | ||
| id: clientId | ||
| parentId: parentClientId | ||
| renderedHtml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
also adds html fallback for unknown blocks